package practice;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 最多购买宝石的数目
 */
public class MaxBuyGemstoneCount {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            int totalCount = Integer.parseInt(sc.nextLine());
            // 宝石价格
            List<Integer> gemstones = new ArrayList<>(totalCount);
            for (int i = 0; i < totalCount; i++) {
                gemstones.add(Integer.parseInt(sc.nextLine()));
            }
            // 总持有的钱
            int value = Integer.parseInt(sc.nextLine());
            int maxCount = getMaxCount(gemstones, value);
            System.out.println("能够买的最大宝石数量为：" + maxCount);
        }
    }

    /**
     * 滑动窗口 求连续元素和大于等于value 要求元素数量最多
     * @param gemstones
     * @param value
     * @return
     */
    private static int getMaxCount(List<Integer> gemstones,int value) {
        int result = 0;
        int start = 0;
        int curr = 0;
        for (int end = 0; end < gemstones.size(); end++) {
            curr+=gemstones.get(end);
            if (curr>value) {
                int count = end-start;
                result = Math.max(count,result);
                curr-=gemstones.get(start);
                start++;
            }
        }
        return result;
    }
}
